package cn.iocoder.yudao.module.design.service.reform;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.reform.vo.ReformCreateReqVO;
import cn.iocoder.yudao.module.design.controller.admin.reform.vo.ReformExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.reform.vo.ReformPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.reform.vo.ReformUpdateReqVO;
import cn.iocoder.yudao.module.design.convert.reform.ReformConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.reform.ReformDO;
import cn.iocoder.yudao.module.design.dal.mysql.reform.ReformMapper;
import cn.iocoder.yudao.module.design.enums.design.ReformStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.REFORM_NOT_EXISTS;

/**
 * 项目整改单 Service 实现类
 *
 * @author czw 
 */
@Service
@Validated
public class ReformServiceImpl implements ReformService {

    @Resource
    private ReformMapper reformMapper;

    @Override
    public Integer createReform(ReformCreateReqVO createReqVO) {
        // 插入
        ReformDO reform = ReformConvert.INSTANCE.convert(createReqVO);
        reformMapper.insert(reform);
        // 返回
        return reform.getId();
    }

    @Override
    public void updateReform(ReformUpdateReqVO updateReqVO) {
        // 校验存在
        validateReformExists(updateReqVO.getId());
        // 更新
        ReformDO updateObj = ReformConvert.INSTANCE.convert(updateReqVO);
        if(ReformStatusEnum.REFORM_STATUS_ENUM_2.getCode().toString().equals(updateReqVO.getStatusCd())){
            updateObj.setStatusCd(ReformStatusEnum.REFORM_STATUS_ENUM_0.getCode().toString());
        }
        reformMapper.updateById(updateObj);
    }

    @Override
    public void deleteReform(Integer id) {
        // 校验存在
        validateReformExists(id);
        // 删除
        reformMapper.deleteById(id);
    }

    private void validateReformExists(Integer id) {
        if (reformMapper.selectById(id) == null) {
            throw exception(REFORM_NOT_EXISTS);
        }
    }

    @Override
    public ReformDO getReform(Integer id) {
        return reformMapper.selectById(id);
    }

    @Override
    public List<ReformDO> getReformList(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return reformMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReformDO> getReformPage(ReformPageReqVO pageReqVO) {
        return reformMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReformDO> getReformList(ReformExportReqVO exportReqVO) {
        return reformMapper.selectList(exportReqVO);
    }

}
